package com.github.postsanf.yinian.slider.library;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.flaviofaria.kenburnsview.KenBurnsView;
import com.github.postsanf.yinian.utils.YNUrlHelper;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by xujiasheng on 16/8/13.
 */
public class NewsAdvAdapter extends PagerAdapter {
    private LayoutInflater inflater;
    private List<String> advertises = null;
    private Context context;

    public NewsAdvAdapter(Context context, List<String> advertises) {
        this.advertises = advertises;
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public void finishUpdate(View arg0) {

    }

    @Override
    public int getCount() {
        return advertises.size();
    }

    @Override
    public Object instantiateItem(ViewGroup view, final int position) {
//		View imageLayout = inflater.inflate(R.layout.item_advpter_image, view,
//				false);
//		TextView textView=(TextView) imageLayout.findViewById(R.id.homepage_news_title);
//		textView.setText(advertises.get(position).getTitle());
//		ImageView imageView = (ImageView) imageLayout
//				.findViewById(R.id.adv_item_iv);
//		imageView.setScaleType(ScaleType.FIT_XY);
//		BitmapManager bmpManager = ApiClient.getInstance().getBitmapManagerForPager(
//				context);
//		if (StringUtils.isEmpty(advertises.get(position).getBigImgUrl())) {
//			imageView.setImageResource(R.drawable.default_image);
//		} else {
//			bmpManager.loadBitmap(advertises.get(position).getBigImgUrl(),imageView);
//		}

//		imageView.setOnClickListener(new OnClickListener() {
//			public void onClick(View v) {
//				UIHelper.showUrlRedirect(context,
//						advertises.get(position).getUrl(), advertises.get(position).getBigImgUrl(), advertises.get(position).getId(),
//						  ImportantNews.class);
//			}
//		});
        KenBurnsView mKenBurnsView = new KenBurnsView(context);
        mKenBurnsView.setLayoutParams(new ViewPager.LayoutParams());
        String url = YNUrlHelper.getCoverUrl(context, advertises.get(position));
        ImageLoader.getInstance().displayImage(url, mKenBurnsView);
        ((ViewPager) view).addView(mKenBurnsView, 0);
        return mKenBurnsView;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {

    }


}
